// frontend/jest.setup.js
// 全局模拟对象
global.alert = jest.fn();
global.console.error = jest.fn();

// 模拟 sessionStorage
const sessionStorageMock = (() => {
  let store = {};
  return {
    getItem: jest.fn((key) => store[key] || null),
    setItem: jest.fn((key, value) => { store[key] = value.toString(); }),
    removeItem: jest.fn((key) => { delete store[key]; }),
    clear: jest.fn(() => { store = {}; }),
  };
})();

Object.defineProperty(window, 'sessionStorage', {
  value: sessionStorageMock,
});

// 模拟 fetch
global.fetch = jest.fn();

// 模拟 window.location
delete window.location;
window.location = { href: '' };